﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.ComponentModel;
using Microsoft.Reporting.WinForms;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace SalesVMT
{
    class ReportPrint
    {
        # region PrintReport
        private int m_currentPageIndex;

        public int CurrentPageIndex
        {
            get { return m_currentPageIndex; }
            set { m_currentPageIndex = value; }
        }
        private IList<Stream> m_streams;

        private Stream CreateStream(string name, string fileNameExtension, Encoding encoding,
                                  string mimeType, bool willSeek)
        {
            Stream stream = new FileStream(name + "." + fileNameExtension, FileMode.Create);
            m_streams.Add(stream);
            return stream;
        }
        public ReportPrint()
        {

        }
        public ReportPrint(LocalReport report)
        {
            Export(report);
        }
        public void Export(LocalReport report)
        {
            string deviceInfo =
              "<DeviceInfo>" +
              "  <OutputFormat>EMF</OutputFormat>" +
              "  <PageWidth>8.26in</PageWidth>" +
              "  <PageHeight>11in</PageHeight>" +
              "  <MarginTop>0in</MarginTop>" +
              "  <MarginLeft>0in</MarginLeft>" +
              "  <MarginRight>0in</MarginRight>" +
              "  <MarginBottom>0in</MarginBottom>" +
              "</DeviceInfo>";

            //            "<DeviceInfo>" +
            //"  <OutputFormat>EMF</OutputFormat>" +
            //"  <PageWidth>8.5in</PageWidth>" +
            //"  <PageHeight>11in</PageHeight>" +
            //"  <MarginTop>0.25in</MarginTop>" +
            //"  <MarginLeft>0.25in</MarginLeft>" +
            //"  <MarginRight>0.25in</MarginRight>" +
            //"  <MarginBottom>0.25in</MarginBottom>" +
            //"</DeviceInfo>";

            Warning[] warnings;
            m_streams = new List<Stream>();
            report.Render("Image", deviceInfo, CreateStream, out warnings);

            foreach (Stream stream in m_streams)
                stream.Position = 0;
        }

        private void PrintPage(object sender, PrintPageEventArgs ev)
        {
            Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
            ev.Graphics.DrawImage(pageImage, ev.PageBounds);

            m_currentPageIndex++;
            ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
        }

        public void Print(Form OwnerForm)
        {
            //const string printerName = "SnagIt 8";
            try
            {
                if (m_streams == null || m_streams.Count == 0)
                    return;

                PrintDialog p = new PrintDialog();
                if (p.ShowDialog(OwnerForm) == DialogResult.OK)
                {
                    PrintDocument printDoc = new PrintDocument();

                    printDoc.PrinterSettings = p.PrinterSettings;
                    if (!printDoc.PrinterSettings.IsValid)
                    {
                        string msg = String.Format("Không tìm thấy máy in \"{0}\".", p.PrinterSettings.PrinterName);
                        MessageBox.Show(msg);
                        return;
                    }
                    printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
                    printDoc.Print();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông báo");
            }
        }

        #endregion
    }
}
